﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace YXShop.TemplateAction
{
    public partial class LabelParse
    {
        public string AricleList()
        {
            string mystyle = this.Label_Inserted;
            string str_ChannelID = this.GetParamValue("YX:ChannelID");//频道ID
            string str_ArticleProperty = this.GetParamValue("YX:ArticleProperty");//文章属性
            string str_ClassID = this.GetParamValue("YX:ClassID");//类别ID
            string str_CompositorType = this.GetParamValue("YX:CompositorType");//排序方式
            string str_CompositorField = this.GetParamValue("YX:CompositorField");//排序字段
            string str_ShowRows = this.GetParamValue("YX:ShowRows");//显示行数
            string str_ShowColumns = this.GetParamValue("YX:ShowColumns");//显示列数
            string str_NameCharacter = this.GetParamValue("YX:NameCharacter");//名称最大字符数
            string str_WhetherPagination = this.GetParamValue("YX:WhetherPagination");//是否分页
            string str_TempletLayout = this.GetParamValue("YX:TempletLayout");//布局方式
            string str_ContentCharacter = this.GetParamValue("YX:ContentCharacter");//内容最大字符数
            string str_WhetherOmit = this.GetParamValue("YX:WhetherOmit");//是否省略
            string str_IsAudit = this.GetParamValue("YX:IsAudit");//是否通过审核
            string str_ShowType = this.GetParamValue("YX:ShowType");//显示类型
            string str_TypeRows = this.GetParamValue("YX:TypeRows");//类型显示行数
            string str_TypeColumns = this.GetParamValue("YX:TypeColumns");//显示类型列数
            YXShop.Common.SysParameter sp = new YXShop.Common.SysParameter();
            int typeRows = 1;
            if (ChangeHope.Common.ValidateHelper.IsNumber(str_TypeRows))
            {
                typeRows = Convert.ToInt32(str_TypeRows);
            }
            int typeColumns = 1;
            if (ChangeHope.Common.ValidateHelper.IsNumber(str_TypeColumns))
            {
                typeColumns = Convert.ToInt32(str_TypeColumns);
            }

            string ChannelId = string.Empty;
            int ShowRows = 1;//显示行
            if (ChangeHope.Common.ValidateHelper.IsNumber(str_ShowRows))
            {
                ShowRows = Convert.ToInt32(str_ShowRows);
            }
            int ShowColumns = 1;//显示列
            if (ChangeHope.Common.ValidateHelper.IsNumber(str_ShowColumns))
            {
                ShowColumns = Convert.ToInt32(str_ShowColumns);
            }
            bool whetheromit = Convert.ToBoolean(str_WhetherOmit);//是否省略
            string where = " 1=1 ";
            string typeId = "0";//查询类别Id
            if (str_ChannelID == "0" && str_ClassID == "0")
            {
                ChannelId = ChangeHope.WebPage.PageRequest.GetQueryString("q_ChannelId");
                string ChannelTypeId = ChangeHope.WebPage.PageRequest.GetQueryString("q_ChannelTypeId");
                if (ChannelTypeId != "")
                {
                    typeId = ChannelTypeId;
                }
                else if (ChannelId != "")
                {
                    typeId = ChannelId;
                }
            }
            else
            {
                //频道类别ID
                if (str_ClassID != "0" && ChangeHope.Common.ValidateHelper.IsNumber(str_ClassID))
                {
                    typeId = str_ClassID;
                }
                else if (str_ClassID == "0")
                {
                    typeId = str_ChannelID;
                }
                ChannelId = str_ChannelID;
            }

            if (!string.IsNullOrEmpty(str_ArticleProperty) && str_ArticleProperty != "0")
            {
                if (ChangeHope.Common.ValidateHelper.IsNumber(str_ArticleProperty))
                {
                    where += " and Substring(Property, " + str_ArticleProperty + ", 1) = 1";
                }
            }

            if (str_IsAudit == "1")
            {
                where += " and State=1";
            }
            else if (str_IsAudit == "2")
            {
                where += " and State=0";
            }


            //排序
            string taxis = "";
            if (str_CompositorType != "")
            {
                if (str_CompositorType == "0")
                {
                    taxis = " asc ";
                }
                else
                {
                    taxis = " desc ";
                }
            }
            //排序字段
            string order = " Id ";
            if (str_CompositorField != "")
            {
                switch (str_CompositorField)
                {
                    case "articleID":
                        order = " Id ";
                        break;
                    case "createTime":
                        order = " CreateTime ";
                        break;
                    case "updateTime":
                        order = " UpdateTime ";
                        break;
                    case "elite":
                        where += " and IsElite=1 ";
                        order = " CreateTime ";
                        break;
                    default:
                        order = " Id ";
                        break;
                }
            }
            //名称长度
            int namelength = 1;
            if (!string.IsNullOrEmpty(str_NameCharacter))
            {
                if (ChangeHope.Common.ValidateHelper.IsNumber(str_NameCharacter))
                {
                    namelength = Convert.ToInt32(str_NameCharacter);
                }
            }

            int contentlength = 10;
            if (ChangeHope.Common.ValidateHelper.IsNumber(str_ContentCharacter))
            {
                contentlength = Convert.ToInt32(str_ContentCharacter);
            }


            StringBuilder shtml = new StringBuilder();
            YXShop.BLL.SystemInfo.Article bll = new YXShop.BLL.SystemInfo.Article();
            ChangeHope.DataBase.DataByPage dataPage = null;

            YXShop.BLL.SystemInfo.ArticleChannel acbll = new YXShop.BLL.SystemInfo.ArticleChannel();
            string strcontent = "";
            int j = 1;
            if (!string.IsNullOrEmpty(mystyle))
            {
                string Str_Tag = "";
                string temptag = "";
                    string pattern = @"{#(.*?)#}";
                    string tempfinallyconent = mystyle;
                    StringBuilder GetTemp = new StringBuilder();
                    MatchCollection macths = Regex.Matches(tempfinallyconent, pattern);

                    foreach (Match macth in macths)
                    {
                        temptag = macth.ToString();
                        int templength = temptag.ToString().IndexOf("{#") + 2;
                        int templength2 = temptag.ToString().IndexOf("#}");
                        if (temptag.ToString().Substring(templength, templength2 - templength).ToString() != null)
                        {
                            Str_Tag = temptag.ToString().Substring(templength, templength2 - templength);
                        }
                    }
                if (str_ShowType == "2")
                {
                    #region 类型与文章都循环
                    ChangeHope.DataBase.DataByPage abdb = acbll.GetList(" [order by] Id desc", typeRows * typeColumns, " and len(Id)=" + (typeId.Length + 3));
                    if (abdb.DataReader != null)
                    {
                        while (abdb.DataReader.Read())
                        {
                            dataPage = bll.GetListByWhereAndOrder(where + " and Channel like '" + abdb.DataReader["Id"].ToString() + "%'", ShowRows * ShowColumns, order + taxis);
                            StringBuilder aricleContent = new StringBuilder();
                            if (Str_Tag != "")
                            {

                                if (dataPage.DataReader != null)
                                {
                                    while (dataPage.DataReader.Read())
                                    {
                                        strcontent = Str_Tag;
                                        if (strcontent.IndexOf("[#Articles_Title]") > -1)
                                        {
                                            if (whetheromit)
                                            {
                                                strcontent = strcontent.Replace("[#Articles_Title]", ChangeHope.Common.StringHelper.SubStringAndAppend(dataPage.DataReader["Title"].ToString(), namelength, "......"));
                                            }
                                            else
                                            {
                                                strcontent = strcontent.Replace("[#Articles_Title]", ChangeHope.Common.StringHelper.SubString(dataPage.DataReader["Title"].ToString(), namelength));
                                            }

                                        }
                                        if (strcontent.IndexOf("[#Articles_Images]") > -1)
                                        {
                                            string imageUrl = string.Empty;
                                            if (dataPage.DataReader["ImagesSoure"].ToString() == "2")
                                            {
                                                imageUrl = dataPage.DataReader["ImagesAddress"].ToString();
                                            }
                                            else
                                            {
                                                imageUrl = sp.DummyPaht + dataPage.DataReader["ImagesAddress"].ToString();
                                            }
                                            if (string.IsNullOrEmpty(imageUrl))
                                            {
                                                imageUrl = sp.DummyPaht + "images/NaughtImages.gif";
                                            }
                                            strcontent = strcontent.Replace("[#Articles_Images]", imageUrl);
                                        }
                                        if (strcontent.IndexOf("[#Articles_SubTitle]") > -1)
                                        {
                                            strcontent = strcontent.Replace("[#Articles_SubTitle]", dataPage.DataReader["SubTitle"].ToString());
                                        }
                                        if (strcontent.IndexOf("[#Articles_Author]") > -1)
                                        {
                                            strcontent = strcontent.Replace("[#Articles_Author]", dataPage.DataReader["Author"].ToString());
                                        }
                                        if (strcontent.IndexOf("[#Articles_SouesURL]") > -1)
                                        {
                                            strcontent = strcontent.Replace("[#Articles_SouesURL]", dataPage.DataReader["LinkUrl"].ToString());
                                        }
                                        if (strcontent.IndexOf("[#Articles_Synopsis]") > -1)
                                        {
                                            if (whetheromit)
                                            {
                                                strcontent = strcontent.Replace("[#Articles_Synopsis]", ChangeHope.Common.StringHelper.SubStringAndAppend(dataPage.DataReader["Introduction"].ToString(), contentlength, "......"));
                                            }
                                            else
                                            {
                                                strcontent = strcontent.Replace("[#Articles_Synopsis]", ChangeHope.Common.StringHelper.SubString(dataPage.DataReader["Introduction"].ToString(), contentlength));
                                            }
                                        }
                                        if (strcontent.IndexOf("[#Articles_Content]") > -1)
                                        {
                                            if (whetheromit)
                                            {
                                                strcontent = strcontent.Replace("[#Articles_Content]", ChangeHope.Common.StringHelper.SubStringAndAppend(dataPage.DataReader["Content"].ToString(), contentlength, "......"));
                                            }
                                            else
                                            {
                                                strcontent = strcontent.Replace("[#Articles_Content]", ChangeHope.Common.StringHelper.SubString(dataPage.DataReader["Content"].ToString(), contentlength));
                                            }
                                        }
                                        if (strcontent.IndexOf("[#Articles_UpdateTime]") > -1)
                                        {
                                            strcontent = strcontent.Replace("[#Articles_UpdateTime]", dataPage.DataReader["UpdateTime"].ToString());
                                        }
                                        if (strcontent.IndexOf("[#Articles_CreateTime]") > -1)
                                        {
                                            strcontent = strcontent.Replace("[#Articles_CreateTime]", dataPage.DataReader["CreateTime"].ToString());
                                        }
                                        string url = string.Empty;
                                        if (dataPage.DataReader["LinkUrl"].ToString() != "")
                                        {
                                            url = dataPage.DataReader["LinkUrl"].ToString();
                                        }
                                        else
                                        {
                                            if (ChannelId == string.Empty)
                                            {
                                                ChannelId = dataPage.DataReader["Channel"].ToString();
                                                if (ChannelId.Length > 2)
                                                {
                                                    ChannelId = ChannelId.Substring(0, 3);
                                                }
                                            }
                                            url = YXShop.TemplateAction.Common.UrlStr("articlecontent", "q_ChannelId=" + ChannelId + ";Id=" + dataPage.DataReader["Id"].ToString() + "", "article/");
                                        }
                                        if (strcontent.IndexOf("[#Articles_URL]") > -1)
                                        {
                                            strcontent = strcontent.Replace("[#Articles_URL]", url);
                                        }
                                        aricleContent.Append(strcontent);

                                        if (!string.IsNullOrEmpty(str_TempletLayout))
                                        {
                                            if (str_TempletLayout == "1")
                                            {
                                                if (j % ShowColumns == 0)
                                                {
                                                    aricleContent.Append("</tr><tr>");
                                                }
                                            }
                                        }
                                        j++;
                                    }
                                }

                            }
                            if (!string.IsNullOrEmpty(str_WhetherPagination))
                            {
                                if (str_WhetherPagination == "0")
                                {
                                    aricleContent.Append(dataPage.ReceptionToolBar);
                                }
                            }
                            dataPage.Dispose();
                            dataPage = null;
                            string strType = mystyle;
                            strType = strType.Replace("[#ClassName]", abdb.DataReader["Name"].ToString());
                            string typeUrl = YXShop.TemplateAction.Common.UrlStr("articlelist", "q_ChannelId=" + ChannelId + ";q_ChannelTypeId=" + abdb.DataReader["Id"].ToString() + "", "article/");
                            strType = strType.Replace("[#Class_Link]", typeUrl);
                            if (temptag != "")
                            {
                                if (aricleContent.ToString() != string.Empty)
                                {
                                    strType = strType.Replace(temptag, aricleContent.ToString());
                                }
                                else
                                {
                                    strType = strType.Replace(temptag, "");
                                }
                            }
                            shtml.Append(strType);
                        }
                        abdb.Dispose();
                        abdb = null;
                    }
                    #endregion 类别与文章都循环
                }
                else
                {
                    StringBuilder aricleContent = new StringBuilder();
                    dataPage = bll.GetListByWhereAndOrder(typeId == "0" ? "" : where + " and Channel like '" + typeId + "%'", ShowRows * ShowColumns, order + taxis);
                    if (dataPage.DataReader != null)
                    {
                        while (dataPage.DataReader.Read())
                        {
                            if (str_ShowType == "1")
                            {
                                strcontent = Str_Tag;
                            }
                            else
                            {
                                if (Str_Tag != "")
                                {
                                    strcontent = Str_Tag;
                                }
                                else
                                {
                                    strcontent = mystyle;
                                }
                            }
                            if (strcontent.IndexOf("[#Articles_Title]") > -1)
                            {
                                if (whetheromit)
                                {
                                    strcontent = strcontent.Replace("[#Articles_Title]", ChangeHope.Common.StringHelper.SubStringAndAppend(dataPage.DataReader["Title"].ToString(), namelength, "......"));
                                }
                                else
                                {
                                    strcontent = strcontent.Replace("[#Articles_Title]", ChangeHope.Common.StringHelper.SubString(dataPage.DataReader["Title"].ToString(), namelength));
                                }

                            }
                            if (strcontent.IndexOf("[#Articles_Images]") > -1)
                            {
                                string imageUrl = string.Empty;
                                if (dataPage.DataReader["ImagesSoure"].ToString() == "2")
                                {
                                    imageUrl = dataPage.DataReader["ImagesAddress"].ToString();
                                }
                                else
                                {
                                    imageUrl = sp.DummyPaht + dataPage.DataReader["ImagesAddress"].ToString();
                                }
                                if (string.IsNullOrEmpty(imageUrl))
                                {
                                    imageUrl = sp.DummyPaht + "images/NaughtImages.gif";
                                }
                                strcontent = strcontent.Replace("[#Articles_Images]", imageUrl);
                            }
                            if (strcontent.IndexOf("[#Articles_SubTitle]") > -1)
                            {
                                strcontent = strcontent.Replace("[#Articles_SubTitle]", dataPage.DataReader["SubTitle"].ToString());
                            }
                            if (strcontent.IndexOf("[#Articles_Author]") > -1)
                            {
                                strcontent = strcontent.Replace("[#Articles_Author]", dataPage.DataReader["Author"].ToString());
                            }
                            if (strcontent.IndexOf("[#Articles_SouesURL]") > -1)
                            {
                                strcontent = strcontent.Replace("[#Articles_SouesURL]", dataPage.DataReader["LinkUrl"].ToString());
                            }
                            if (strcontent.IndexOf("[#Articles_Synopsis]") > -1)
                            {
                                if (whetheromit)
                                {
                                    strcontent = strcontent.Replace("[#Articles_Synopsis]", ChangeHope.Common.StringHelper.SubStringAndAppend(dataPage.DataReader["Introduction"].ToString(), contentlength, "......"));
                                }
                                else
                                {
                                    strcontent = strcontent.Replace("[#Articles_Synopsis]", ChangeHope.Common.StringHelper.SubString(dataPage.DataReader["Introduction"].ToString(), contentlength));
                                }
                            }
                            if (strcontent.IndexOf("[#Articles_Content]") > -1)
                            {
                                if (whetheromit)
                                {
                                    strcontent = strcontent.Replace("[#Articles_Content]", ChangeHope.Common.StringHelper.SubStringAndAppend(dataPage.DataReader["Content"].ToString(), contentlength, "......"));
                                }
                                else
                                {
                                    strcontent = strcontent.Replace("[#Articles_Content]", ChangeHope.Common.StringHelper.SubString(dataPage.DataReader["Content"].ToString(), contentlength));
                                }
                            }

                            if (strcontent.IndexOf("[#Articles_UpdateTime]") > -1)
                            {
                                strcontent = strcontent.Replace("[#Articles_UpdateTime]", dataPage.DataReader["UpdateTime"].ToString());
                            }
                            if (strcontent.IndexOf("[#Articles_CreateTime]") > -1)
                            {
                                strcontent = strcontent.Replace("[#Articles_CreateTime]", dataPage.DataReader["CreateTime"].ToString());
                            }
                            string url = string.Empty;
                            if (dataPage.DataReader["LinkUrl"].ToString() != "")
                            {
                                url = dataPage.DataReader["LinkUrl"].ToString();
                            }
                            else
                            {
                                if (ChannelId == string.Empty)
                                {
                                    ChannelId = dataPage.DataReader["Channel"].ToString();
                                    if (ChannelId.Length > 2)
                                    {
                                        ChannelId = ChannelId.Substring(0, 3);
                                    }
                                }
                                url = YXShop.TemplateAction.Common.UrlStr("articlecontent", "q_ChannelId=" + ChannelId + ";Id=" + dataPage.DataReader["Id"].ToString() + "", "article/");
                            }
                            if (strcontent.IndexOf("[#Articles_URL]") > -1)
                            {
                                strcontent = strcontent.Replace("[#Articles_URL]", url);
                            }
                            aricleContent.Append(strcontent);
                            if (!string.IsNullOrEmpty(str_TempletLayout))
                            {
                                if (str_TempletLayout == "1")
                                {
                                    if (j % ShowColumns == 0)
                                    {
                                        aricleContent.Append("</tr><tr>");
                                    }
                                }
                            }
                            j++;
                        }
                    }
                    StringBuilder contentHtml = new StringBuilder();
                    if (aricleContent.ToString() != null)
                    {
                        if (temptag!="")
                        {
                            mystyle = mystyle.Replace(temptag, aricleContent.ToString());
                            contentHtml.Append(mystyle);
                        }
                        else
                        {
                            contentHtml.Append(aricleContent.ToString());
                        }

                    }
                    
                    if (!string.IsNullOrEmpty(str_WhetherPagination))
                    {
                        if (str_WhetherPagination == "0")
                        {
                            contentHtml.Append(dataPage.ReceptionToolBar);
                        }
                    }
                    dataPage.Dispose();
                    dataPage = null;
                    if (str_ShowType == "1")
                    {
                        string strType = contentHtml.ToString();
                        YXShop.Model.SystemInfo.ArticleChannel acModel = acbll.GetModel(typeId);

                        if (acModel != null)
                        {
                            string typeUrl = YXShop.TemplateAction.Common.UrlStr("articlelist", "q_ChannelId=" + ChannelId + ";q_ChannelTypeId=" + acModel.Id + "", "article/");
                            strType = strType.Replace("[#Class_Link]", typeUrl);
                            if (strType.IndexOf("[#ClassName]") > -1)
                            {
                                strType = strType.Replace("[#ClassName]", acModel.Name);
                            }
                        }
                        else
                        {
                            if (strType.IndexOf("[#ClassName]") > -1)
                            {
                                strType = strType.Replace("[#ClassName]", "");
                            }
                        }
                        //if (aricleContent.ToString() != null && temptag.Length > 0)
                        //{
                        //    strType = strType.Replace(temptag, aricleContent.ToString());
                        //}
                        shtml.Append(strType);
                    }
                    else
                    {
                        //if (aricleContent.ToString() != null && temptag.Length > 0)
                        //{
                        //    mystyle = mystyle.Replace(temptag, aricleContent.ToString());
                        //}
                        shtml.Append(contentHtml.ToString());

                        //shtml.Append(aricleContent.ToString());
                    }
                }
            }
            return shtml.ToString();
        }
    }
}
